<template>
  <mu-container>
    <mu-appbar style="width: 100%;">
      <mu-button icon slot="left" @click='$root.back'>
        <mu-icon value="arrow_back"></mu-icon>
      </mu-button>
      Demo
    </mu-appbar>
    <div style="padding-bottom: 120px;">
      <!-- <mu-container class="button-wrapper"> -->
      <mu-button color="primary" @click='human'>人机验证</mu-button>
      <mu-button color="primary" @click='automaticLogin'>自动登录</mu-button>
      <mu-button color="secondary" @click='manualLogin'>手动登录</mu-button>
      <mu-button color="success" @click='register'>注册</mu-button>
      <!-- </mu-container> -->
      <mu-button color="success" @click="payTest">支付0.01元</mu-button>
      <mu-button color="success" @click="location">定位</mu-button>
      <mu-button color="success" @click="chooseImage">选择图片</mu-button>
      <mu-button color="success" @click="zhibo">进入直播</mu-button>
      <mu-button color="success" @click="share">分享</mu-button>
      <mu-button color="success" @click="shareReg">分享注册</mu-button>
      <mu-button color="success" @click="backPressOn">{{backPressOning ? '取消监听backPress' : '监听backPress'}}</mu-button>
      <mu-button color="success" @click="backHome">返回首页</mu-button>
      <mu-button color="success" @click="backNav">返回</mu-button>
      <mu-button color="success" @click="scanCode">扫一扫</mu-button>
      <mu-button color="success" @click="help">客服</mu-button>

      <mu-button color="primary" @click="loginAct">登录回调</mu-button>
      <mu-button color="primary" @click="shareRegAct">分享注册回调</mu-button>
      <mu-button color="primary" @click="payAct">支付回调</mu-button>
      <mu-button color="primary" v-if="$root.payedOrderId" @click="refoundAct">退款回调</mu-button>
      <mu-button color="primary" @click="addrAct">选择收货地址回调</mu-button>

      <mu-button color="primary" @click="addrCurrAct">默认收货地址(定位)回调</mu-button>
      <mu-button color="primary" @click="addrCurrAct2">默认收货地址(普通)回调</mu-button>

      <mu-button color="primary" @click="localtionAct">定位回调</mu-button>

      <mu-button color="primary" @click="yxAct">跳转乙信</mu-button>

      <mu-button color="primary" @click="uploadAct">上传资源</mu-button>

      <mu-button color="primary" @click="fullTest">全屏切换</mu-button>

      <mu-button color="primary" @click="videoTest">播放视频</mu-button>

      <!-- <mu-button color="primary" @click="videoTest">播放视频</mu-button> -->
      <mu-button color="primary" @click="entryTest">嵌套调用</mu-button>

    </div>
    <Bar class='bar'></Bar>
  </mu-container>

</template>

<script>
  import config from '../config'
  import Bar from '@/components/bar.vue'

  export default {
    components: {
      'Bar': Bar
    },
    data() {
      return {
        token: this.$root.token,
        pToken: '', //应用方保留在平台的授权信息
        backPressOning: false,
      }
    },
    computed: {

    },
    created() {
      window.automaticLogin = function(token) {
        console.log('dev login ' + token)
      }
    },
    methods: {
      human() {
        $abAct('human', 0, function() {
          console.log(arguments)
        })
      },
      
      //调用回调接口
      callbacks() {
        var _this = this;
        _this.$root.pTokenBack();
      },
      //自动登录
      automaticLogin() {
        var _this = this;
        _this.$root.autoLogin();
      },
      //手动登录
      manualLogin() {
        var _this = this;
        if (this.$root.token) {
          _this.$router.push({
            path: '/pages/manualLogin'
          })

        } else {
          $postMessage('login')
        }
      },
      //注册
      register() {
        var _this = this;
        _this.$router.push({
          path: '/pages/register'
        })
      },

      payTest() {
        this.$root.pay(1, '0.01支付', '测试支付0.01元')
      },

      location() {
        $uniAct('getLocation', {
          altitude: true,
          success: function() {
            console.log('getLocation success')
          },
          fail: function() {
            console.log('getLocation fail')
          },
          complete: function() {
            console.log('getLocation complete')
          },
        })
      },

      chooseImage() {
        $uniAct('chooseImage', {
          altitude: true,
          success: function(res) {
            console.log('chooseImage success')
            console.log(res.tempFilePaths)
          },
          fail: function() {
            console.log('chooseImage fail')
          },
          complete: function() {
            console.log('chooseImage complete')
          },
        })
      },

      zhibo() {
        $postMessage('zhibo', 145)
      },

      share() {
        $postMessage('share', JSON.stringify({
          href: location.href,
          imageUrl: 'https://plt-test.oss-cn-shanghai-finance-1-pub.aliyuncs.com/2019/08/12/a1ghvutg0nhcmwlx.png',
          title: '测试分享',
          summary: '测试分享更多'
        }))
      },

      shareReg() {
        $postMessage('shareReg', JSON.stringify({
          //href: location.href,
          imageUrl: 'https://plt-test.oss-cn-shanghai-finance-1-pub.aliyuncs.com/2019/08/12/a1ghvutg0nhcmwlx.png',
          title: '测试分享',
          summary: '测试分享更多',
          reg: {
            uri: 'https://www.baidu.com',
          }
        }))
      },

      backPressOn() {
        this.backPressOning = !this.backPressOning
        $postMessage('backPressOn', this.backPressOning)
      },

      backHome() {
        $postMessage('backHome')
      },

      backNav() {
        $postMessage('backProj')
      },

      scanCode() {
        $uniAct('scanCodeP', {
          success: function(res) {
            console.log('scanCodeP')
            console.log(res)
          },
          fail: function() {
            console.log('scanCodeP fail')
          },
          complete: function() {
            console.log('scanCodeP complete')
          },
        })
      },

      help() {
        $postMessage('help', JSON.stringify([{
          'index': 1,
          'key': 'tag',
          'label': '测试标签',
          'value': '测试值Test'
        }]))
      },

      loginAct() {
        $abAct('login', 1, function(err, token) {
          console.log("loginAct err = " + err)
          console.log("loginAct back = " + token)
        })
      },

      shareRegAct() {
        $abAct('shareReg', JSON.stringify({}), function(err, token) {
          console.log("shareReg err = " + err)
          console.log("shareReg back = " + token)
        })
      },

      payAct() {
        var self = this.$root
        if (!self.loginInfo) {
          $postMessage('login')
          return
        }

        var ab_ = self.ab_
        var oId = new Date().getTime()
        ab_.rpc(1, ab_.pr('O/order'), {
          mmId: self.loginInfo.member.id,
          oId: oId,
          name: '测试支付',
          desc: '测试支付',
          amount: 1,

        }, function(err, res) {
          // self.err_ += $postMessage
          // uni.navigateTo({
          // 	url: '/pages/transfer?t=pay,' + res
          // })
          if (res) {
            $abAct('pay', res, function(err, token) {
              if (token) {
                $root.payedOrderId = oId
              }
              console.log("pay err = " + err)
              console.log("pay back = " + token)
            })
          }
        })
      },

      refoundAct() {
        if ($root.payedOrderId) {
          this.ab_.rpc(1, this.ab_.pr('O/refund'), $root.payedOrderId, 1, function(err, rep) {
            $root.$toast.error('退款' + err + ', ' + rep);
            console.log('退款' + err + ', ' + rep)
          })
        }
      },

      addrAct() {
        $abAct('address', 0, function(err, token) {
          console.log("address err = " + err)
          console.log("address back = " + token)
        })
      },

      addrCurrAct() {
        $abAct('addressCurr', 1, function(err, token) {
          console.log("address err = " + err)
          console.log("address back = " + JSON.stringify(token))
          $root.$toast.error(JSON.stringify(token));
        })
      },
      addrCurrAct2() {
        $abAct('addressCurr', 2, function(err, token) {
          console.log("address err = " + err)
          console.log("address back = " + JSON.stringify(token))
                      $root.$toast.error(JSON.stringify(token));
        })
      },
      localtionAct() {
          $abAct('location', 2, function(err, token) {
            console.log("address err = " + err)
            console.log("address back = " + JSON.stringify(token))
                        $root.$toast.error(JSON.stringify(token));
          })
      },
      yxAct() {
        $postMessage('navYx')
      },

      uploadAct() {
        /**
         * 上传资源 演示
         */
        $abAct('upload', JSON.stringify({
          // 1 只是视频 2 只是图片
          type: 0,
          // 二级分类（行业分类）
          catId2: 0,
          // 三级分类 （20个模块分类）
          catId3: 0,

        }), function(err, article) {
          console.log("upload err = " + err)
          console.log("upload back = " + JSON.stringify(article))
        })
      },

      fullTest() {
        var self = this
        $abAct('fullScreen', self.fullScreen ? -1 : 1,
          function(err, topBar) {
            self.fullScreen = !self.fullScreen
            console.log('topBar = ' + topBar)
          })
      },

      videoTest() {
        var player = plus.video.createVideoPlayer('videoplayer', {
          src: 'http://shipin.oss.yiyiny.com/2019/10/25/br92nkjusld3bqo1.mp4',
          top: '100px',
          left: '0px',
          width: '100%',
          height: '200px',
          position: 'static'
        });
        plus.webview.currentWebview().append(player);
      },

      entryTest() {
        if(location.href.startsWith('http://139.224.149.225')) {
          $postMessage('uri,http://plt.test.yiyitop.com/plt-demo/#/pages/chooseWay')

        } else {
          $postMessage('uri,http://139.224.149.225/plt-demo/#/pages/chooseWay')
        }
      },
    },
  }
</script>

<style>
  .container {
    padding-left: 0px !important;
    padding-right: 0px !important;
  }

  .mu-button {
    text-align: center;
    width: 55%;
  }

  .mu-inverse {
    margin-top: 15%;
  }

  .mu-appbar-title {
    padding-left: 0px !important;
    padding-right: 3.125rem !important;
  }

  .bar {
    position: fixed;
    bottom: 0px;
  }
</style>
